/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test;

import static org.junit.Assert.assertEquals;
import static org.sourceprojects.lycia.fluent.FluentBuilder.method;
import static org.sourceprojects.lycia.fluent.FluentBuilder.prepare;
import static org.sourceprojects.lycia.fluent.FluentBuilder.tag;

import java.lang.reflect.Method;

import org.junit.Test;
import org.sourceprojects.lycia.DefinitionBuilder;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.ParserContext;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.w3c.dom.Element;

public class FluentConfigurationTestCase {
	private static final String XML_DATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xml><input id=\"5\" /></xml>";

	@Test
	public void testFluentConfiguration() throws Exception {
		final FluentBuilder<StringBuilder> builder = prepare();

		final Method method = FluentConfigurationTestCase.class.getMethod(
				"parseInput", Element.class, ParserContext.class,
				DefinitionBuilder.class);

		builder.parser(tag("input", method(method, this)));

		final LyciaParser<StringBuilder> parser = builder.build();
		parser.getParserContext().setContextObject(new StringBuilder());
		parser.parse(XML_DATA);

		assertEquals("5", parser.getParserContext().getContextObject()
				.toString());
	}

	public void parseInput(final Element element,
			final ParserContext<StringBuilder> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {

		final StringBuilder buffer = parserContext.getContextObject();
		buffer.append(element.getAttribute("id"));
	}

}
